package com.younion.common.paginator;

/**
 * 分页器，包含分页所需要的信息
 * 
 * @author wells
 */
public class Paginator {

	private int pageSize = 30; // 默认每页记录数

	private int totalCount = 0; // 总记录数

	private int pageIndex = 0; // 当前页号3

	private int index = 0; // 当前记录序号46=3*15+（0+1）

	public Paginator() {

	}

	public int getPageIndex() {
		return totalCount > 0 ? pageIndex + 1 : pageIndex;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex - 1;
	}

	public void setTotalCount(int totalCount) {

		this.totalCount = totalCount;

		pageIndex = (pageIndex == getTotalPage()) ? ((pageIndex == 0) ? pageIndex : pageIndex - 1)
				: pageIndex;

	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStartIndex() {
		return pageSize * pageIndex;
	}

	public int getIndex() {
		return pageSize * pageIndex + ++index;
	}

	public int getEndIndex() {
		return pageSize * (pageIndex + 1);
	}

	public int getTotalPage() {
		return (int) Math.ceil((float) getTotalCount() / (float) pageSize);
	}
}
